package UIFree.controls
{
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class uif_tabItem extends uif_label
	{
		public function uif_tabItem(parent:uif_component, rect: Rectangle, styleClass:String='') {
			super(parent, rect.x, rect.y, rect.width, rect.height, styleClass);
			this.addEventListener(MouseEvent.CLICK, doRelease);
		}
		
		protected function doRelease(event: MouseEvent): void {
			tab.selectIndex = index;
		}
		
		public function get tab(): uif_tabs {
			return parent.parent as uif_tabs;
		}
		
		override public function get index(): int {
			return tab.findControl(this);
		}
		
		protected function draw_tab(rect: Rectangle, a_style: Object): void {
			begin2(a_style);
			
			var rounds: Array = getRounds(a_style);
			var rs: Number = rounds[0] / 2;
			
			var sc: int = Math.round(rect.height / 2) - rs;
			graphics.moveTo(rect.x, rect.y + rect.height);
			graphics.lineTo(rect.x + sc, rect.y + rs);
			graphics.curveTo(rect.x + sc + rs, rect.y, rect.x + sc + rounds[0], rect.y);
			
			graphics.lineTo(rect.x + rect.width - sc - rounds[0], rect.y);
			
			graphics.curveTo(rect.x + rect.width - sc - rs, rect.y, rect.x + rect.width - sc, rect.y + rs);
			
			graphics.lineTo(rect.x + rect.width, rect.y + rect.height);
			graphics.lineStyle(1, 0, 0);
			graphics.lineTo(rect.x, rect.y + rect.height);
			if (style.fill_color) this.graphics.endFill();
		} 
		
		override public function getInner(incx: Number=0, incy: Number=0, incw:Number=0, inch:Number=0): Rectangle {
			var rect: Rectangle = super.getInner(incx, incy, incw, inch);
			rect.inflate(-Math.round(rect.height / 2), 0);
			return rect;
		}
		
		override protected function paint(a_styleClass: String, a_method: String = ''): void {
			var rect: Rectangle = getBorder();
			draw_tab(rect, app.styleByName(a_styleClass));
		}
	}
}